package Test;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import entity.Demo;

public class DemoTest {
	/*public static void main(String[] args) {
		   //Demo demo=new Demo(); 
		Class<?> demo=null; 
		   try {
			demo=Class.forName("entity.Demo");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Demo demo1=null;
		
		
			Class<?>[] interfaces = demo.getInterfaces();
			Class<?> superclass = demo.getSuperclass();
			Constructor<?>[] con = demo.getConstructors();
			for(int i=0;i<con.length;i++){
				System.out.println(con[i]);
			}
			System.out.println(superclass.getName());
			System.out.println(demo.getSimpleName());
			for(Class in:interfaces){
				System.out.println("接口"+in.getName());
			}
		
		  demo1.setAge("20");
		  demo1.setName("笑话");
		  System.out.println(demo1.toString());
		   
	}*/
	public static void main(String[] args) throws InstantiationException {
        int[] temp={1,2,3,4,5,6,7,8,9};
        int[] newTemp=(int[])arrayInc(temp,15);
        print(newTemp);
        System.out.println("=====================");
        String[] atr={"a","b","c"};
        String[] str1=(String[])arrayInc(atr,8);
        print(str1);
		Demo demo=new Demo("小米","28");
		 try {
			Constructor<? extends Demo> de = demo.getClass().getDeclaredConstructor(String.class,String.class);
			Field field = demo.getClass().getDeclaredField("name");
			field.set(demo, "花花");
			System.out.println(field.get(demo));
			demo.getClass().newInstance();
			Method method = demo.getClass().getMethod("getMe", String.class,String.class);
			//method.invoke(demo);
			System.out.println(method.invoke(demo,new Object[]{new String("阿花"),new String("28")}));
			Class<?>[] para = method.getParameterTypes();
			for(int i=0;i<para.length;i++){
				System.out.println(para[i]);
			}
			
			//System.arraycopy(src, srcPos, dest, destPos, length);
			//System.out.println(method.getReturnType());
			//System.out.println(method.getExceptionTypes());
			//System.out.println(de.toString());
			System.out.println(field);
		} catch (NoSuchMethodException | SecurityException | NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Field[] fields = demo.getClass().getDeclaredFields();//获取本类中的属性值
		for(Field fi:fields){
			System.out.println(Modifier.toString(fi.getModifiers())+"----"+fi.getType()+"--------"+fi.getName());
		}
    }
    
    /**
     * 修改数组大小
     * */
    public static Object arrayInc(Object obj,int len){
        Class<?>arr=obj.getClass().getComponentType();//先得到arr的类型
        Object newArr=Array.newInstance(arr, len);
        int co=Array.getLength(obj);
        System.arraycopy(obj, 0, newArr, 0, Math.min(co, len));
        return newArr;
    }
    /**
     * 打印
     * */
    public static void print(Object obj){
        Class<?>c=obj.getClass();
        if(!c.isArray()){
            return;
        }
        System.out.println("数组长度为： "+Array.getLength(obj));
        for (int i = 0; i < Array.getLength(obj); i++) {
            System.out.print(Array.get(obj, i)+" ");
        }
    }
}
